<?php

/**
 * Description of SignupService
 *
 * @author Adam
 */
class SignupService {
    private static $instance = null;

    public static function getInstance() {
       if (!self::$instance instanceof self) {
         self::$instance = new self;
       }
       return self::$instance;
    }

    public function __destruct() {
    }

    public function getUserInfoByUsername($DBprefix, $name) {
        $name = mysql_escape_string($name);
        $query = "SELECT * FROM `".$DBprefix."signup` WHERE `user_name` = '".$name."'";
        $queryResult = mysql_query($query);
        
        $user = new Signup();
        if ($user_info = mysql_fetch_array($queryResult)){ 
            $user->setId( $user_info["user_id"] );
            $user->setUsername( $user_info["user_name"] );
            $user->setFname( $user_info["user_fname"] );
            $user->setLname( $user_info["user_lname"] );
            $user->setPhone( $user_info["user_phone"] );
            $user->setEmail( $user_info["user_email"] );
            $user->setLevel( $user_info["user_level"] );
            $user->setAddDate( date("d.m.Y h:iA",$user_info["user_adddate"]) );
            $user->setLastLogin( $user_info["user_lastlogin"] );
            $user->setLoginDns( $user_info["user_logindns"] );
            $user->setLoginFail( date("d.m.Y h:iA",$user_info["user_loginfail"]) );
            $user->setSetTimeout( $user_info["user_set_timeout"] );
        }
        
        return $user;
    }

    public function getUserInfo($DBprefix, $name, $pass) {
        $name = mysql_escape_string($name);
        $pass = mysql_escape_string($pass);
        $query = "SELECT * FROM `".$DBprefix."signup` WHERE `user_name` = '".$name."' AND `user_pass` = '".$pass."'";
        $queryResult = mysql_query($query);
        
        $user = new Signup();
        if ($user_info = mysql_fetch_array($queryResult)){ 
            $user->setId( $user_info["user_id"] );
            $user->setUsername( $user_info["user_name"] );
            $user->setFname( $user_info["user_fname"] );
            $user->setLname( $user_info["user_lname"] );
            $user->setPhone( $user_info["user_phone"] );
            $user->setEmail( $user_info["user_email"] );
            $user->setLevel( $user_info["user_level"] );
            $user->setAddDate( date("d.m.Y h:iA",$user_info["user_adddate"]) );
            $user->setLastLogin( $user_info["user_lastlogin"] );
            $user->setLoginDns( $user_info["user_logindns"] );
            $user->setLoginFail( date("d.m.Y h:iA",$user_info["user_loginfail"]) );
            $user->setSetTimeout( $user_info["user_set_timeout"] );
        }
        
        return $user;
    }

    public function getUserInfoById($DBprefix, $idUser) {
        $idUser = mysql_escape_string($idUser);
        $query = "SELECT * FROM `".$DBprefix."signup` WHERE `user_id` = '$idUser'";
        $queryResult = mysql_query($query);
        
        $user = new Signup();
        if ($user_info = mysql_fetch_array($queryResult)){ 
            $user->setId( $user_info["user_id"] );
            $user->setUsername( $user_info["user_name"] );
            $user->setFname( $user_info["user_fname"] );
            $user->setLname( $user_info["user_lname"] );
            $user->setPhone( $user_info["user_phone"] );
            $user->setEmail( $user_info["user_email"] );
            $user->setLevel( $user_info["user_level"] );
            $user->setAddDate( date("d.m.Y h:iA",$user_info["user_adddate"]) );
            $user->setLastLogin( $user_info["user_lastlogin"] );
            $user->setLoginDns( $user_info["user_logindns"] );
            $user->setLoginFail( date("d.m.Y h:iA",$user_info["user_loginfail"]) );
            $user->setSetTimeout( $user_info["user_set_timeout"] );
        }
        
        return $user;
    }

    public function getUserCountByIdAndSessionId($DBprefix, $idUser, $idSes) {
        $idUser = mysql_escape_string($idUser);
        $query = "SELECT * FROM `".$DBprefix."logs` WHERE `log_sesid` = '$idSes' AND `log_userid` = '$idUser'";
        $queryResult = mysql_query($query);
        $count = mysql_num_rows($queryResult);        
        return $count;
    }

    public function count($DBprefix) {
        $idUser = mysql_escape_string($idUser);
        $query = "SELECT * FROM `" . $DBprefix . "signup`";
        $queryResult = mysql_query($query);
        $count = mysql_num_rows($queryResult);        
        return $count;
    }
    
    public function getAllLimit($DBprefix,$userId,$begin,$onPage) {
        $query = "SELECT * FROM `" . $DBprefix . "signup` ORDER BY `$userId` DESC LIMIT $begin,$onPage";
        $result = mysql_query($query);
        
        $users = array();
        while ($user_info = mysql_fetch_array($result)){
            $user = new Signup();
            $user->setId( $user_info["user_id"] );
            $user->setUsername( $user_info["user_name"] );
            $user->setFname( $user_info["user_fname"] );
            $user->setLname( $user_info["user_lname"] );
            $user->setPhone( $user_info["user_phone"] );
            $user->setEmail( $user_info["user_email"] );
            $user->setLevel( $user_info["user_level"] );
            $user->setAddDate( date("d.m.Y h:iA",$user_info["user_adddate"]) );
            $user->setLastLogin( $user_info["user_lastlogin"] );
            $user->setLoginDns( $user_info["user_logindns"] );
            $user->setLoginFail( date("d.m.Y h:iA",$user_info["user_loginfail"]) );
            $user->setSetTimeout( $user_info["user_set_timeout"] );
            $users[] = $user;
        }
        
        return $users;
    }
}

?>
